package com.cysion.otbox.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import com.cysion.otbox.listener.OnTypeClickListener

abstract class BaseAdapter<T : Any>(
    var mEntities: MutableList<T>,
    var mContext: Context,
    protected var mOnTypeClickListener: OnTypeClickListener<T>?
) : RecyclerView.Adapter<BaseViewHolder<T>>() {

    //处理交互事件
    override fun getItemCount(): Int {
        return mEntities.size
    }

    fun getItem(pos: Int): T {
        if(pos<0||pos>mEntities.size) throw Exception()
        return mEntities[pos]
    }

    fun addItem(list:List<T>){
        mEntities.addAll(list)
        notifyDataSetChanged()
    }

    fun setItems(list:List<T>){
        if(list.isEmpty()) return
        mEntities.clear()
        mEntities.addAll(list)
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: BaseViewHolder<T>, position: Int) {
        holder.bindData(mContext, mOnTypeClickListener, mEntities[position], position)
    }

    protected open fun inflate(parent: ViewGroup?, @LayoutRes resource: Int): View {
        return LayoutInflater.from(mContext).inflate(resource, parent, false)
    }

    override abstract fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<T>
}

//条目单击事件，
const val ITEM_CLICK = -100000

//长按事件
const val ITEM_LONG_CLICK = -100001
